package cn.jdy.plugins.wap;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.plugins.entity.VisitLog;
import cn.jdy.plugins.service.VisitLogService;

/**
 * @title  用户访问日志
 * @author ningqian
 * @since  2017-5-16
 */

@Controller
@RequestMapping("/visitLog")
public class VisitLogCtrl extends BaseCtrl{
	
	@Autowired
	private VisitLogService visitLogService;     
	
	
	
	/**
	 * 退出时记录访问日志
	 * @param orderNo
	 * @return
	 */
	@RequestMapping("saveVisitLog") 
	@ResponseBody
    public Map<String,String> saveVisitLog(@RequestParam("liveId") Long liveId,@RequestParam("userOpenId") String userOpenId,
    		@RequestParam("userNickName") String userNickName,@RequestParam("userHeadImg") String userHeadImg,
    		@RequestParam("userSex") String userSex,@RequestParam("operateDesc") String operateDesc,
    		@RequestParam("status") String status) {
		Map<String,String> map = new HashMap<String,String>();
		String result ="ok";
		try {
			addVisitLog(liveId, userOpenId, userNickName, userHeadImg, userSex, operateDesc, status);
		} catch (Exception e) {
			result = "error";
		}
		map.put("result", result);
		return map; 
	}
	
	
	
	/**
	 * 添加用户访问日志
	 * 
	 * @param operateDesc
	 * @param status 1-在线 2-离线
	 */
	public void addVisitLog(Long liveId, String uid, String userNickName,String userHeadImg,
			String userSex, String operateDesc,String status) {
		VisitLog visitLog = new VisitLog();
		visitLog.setLiveId(liveId);
		visitLog.setUserOpenId(uid);
		visitLog.setUserNickName(userNickName);
		visitLog.setUserHeadImg(userHeadImg);
		visitLog.setUserSex(userSex);
		visitLog.setOperateDesc(operateDesc);
		visitLog.setStatus(status);
		visitLog.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
		try {
			visitLogService.save(visitLog);
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "添加用户访问日志异常");
		}

	}
	
}
